package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.self.AddressAdapter;
import com.sgb.kjwl.view.ui.adapter.self.AreaAdapter;
import com.sgb.kjwl.view.ui.adapter.self.CityAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class AddressPopWindow extends BasePopWindow {

    private RecyclerView provincialRecyclerView;
    private RecyclerView cityRecyclerView;
    private RecyclerView areaRecyclerView;
    private TextView okView;
    private TextView resetView;
    private LinearLayout bottomLayout;
    private AddressAdapter addressAdapter;
    private CityAdapter cityAdapter;
    private AreaAdapter areaAdapter;
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas;
    private AddressEntity<List<AddressEntity<List<AddressEntity>>>> addressEntitys;

    public AddressPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_reg_address;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        provincialRecyclerView = holder.getPop().findViewById(R.id.provincial_list);
        cityRecyclerView = holder.getPop().findViewById(R.id.city_list);
        areaRecyclerView = holder.getPop().findViewById(R.id.area_list);

//        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        bottomLayout = holder.getPop().findViewById(R.id.bottom_layout);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onResultClick != null) {
                    if (addressEntitys != null) {
                        onResultClick.result(addressEntitys, null,null);
                        Log.d("经营范围：", addressEntitys.getName());
                    }
                }
            }
        });

        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        addressAdapter = new AddressAdapter(mContext, null);
        addressAdapter.setItemSelect(true);
        addressAdapter.setDefItem(-1);
        provincialRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        provincialRecyclerView.setAdapter(addressAdapter);
        addressAdapter.setOnItemClick(new AdapterOnItemClick<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>() {
            @Override
            public void onItemClick(AddressEntity<List<AddressEntity<List<AddressEntity>>>> addressEntity, int position) {
                addressEntitys = addressEntity;
                cityAdapter.setDatas(addressEntity.getChildren());
            }
        });

        cityAdapter = new CityAdapter(mContext, null);
        cityAdapter.setItemSelect(true);
        cityAdapter.setDefItem(-1);
        cityRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        cityRecyclerView.setAdapter(cityAdapter);
        cityAdapter.setOnItemClick(new AdapterOnItemClick<AddressEntity<List<AddressEntity>>>() {
            @Override
            public void onItemClick(AddressEntity<List<AddressEntity>> addressEntity, int position) {
//                addressEntitys = addressEntity;
                areaAdapter.setDatas(addressEntity.getChildren());
            }
        });

        areaAdapter = new AreaAdapter(mContext, null);
        areaAdapter.setItemSelect(true);
        areaAdapter.setDefItem(-1);
        areaRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        areaRecyclerView.setAdapter(areaAdapter);
        areaAdapter.setOnItemClick(new AdapterOnItemClick<AddressEntity>() {
            @Override
            public void onItemClick(AddressEntity addressEntity, int position) {
//                addressEntitys = addressEntity;
            }
        });
    }

    public void setData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        this.datas = datas;
        addressAdapter.setDatas(datas);
        cityAdapter.setDatas(datas.get(0).getChildren());
        areaAdapter.setDatas(datas.get(0).getChildren().get(0).getChildren());
    }
}
